<?php

namespace App\Models;

use App\MongoBaseModel as Model;
use Helper;

/**
 * Class SimCard
 * @package App\Models
 * @property string $id
 * @property string $mobile
 * @property string $index
 * {
 *     @property int $book
 *     @property int $page
 *     @property int $box
 * }
 * @property int $indexCache
 * @property string $company_id
 * @property string $created_at
 * @property string $updated_at
 */
class SimCard extends Model
{
    protected $fillable = [
        'mobile', 'company_id', 'index', 'indexCache'
    ];
    protected $appends = ['hasWxClient', 'companyName'];

    public function getHasWxClientAttribute() {
        if ($this->mobile) {
            return (bool) WxClient::where('mobile', $this->mobile)->count();
        }
        return false;
    }

    public function getCompanyNameAttribute() {
        if ($this->company_id) {
            $company = Company::find($this->company_id);
            if ($company) {
                return $company->name;
            }
            return "";
        }
        return "";
    }

    public function convertIndex() {
        return $this->index['book'] * 10000 +
        $this->index['page'] * 100 + $this->index['box'];
    }

    public function scopeList($query, $inputs) {
        if(!empty($inputs)){
            return $query->where(function ($querys) use ($inputs){
                if (isset($inputs['mobile']) && $inputs['mobile'] === ""){
                    $querys->where([Helper::makeMongoEmptyFilter('mobile')]);
                }
                if (isset($inputs['wxClient_id']) && $inputs['wxClient_id'] === ""){
                    $querys->where([Helper::makeMongoEmptyFilter('wxClient_id')])
                        ->where([
                            'mobile' => [
                                '$exists' => true,
                                '$nin' => [0, "", null],
                            ]
                        ]);
                }
                if (!empty($inputs['book'])) {
                    $querys->where('index.book', $inputs['book']);
                }
                if (!empty($inputs['page'])) {
                    $querys->where('index.page', $inputs['page']);
                }
                if (!empty($inputs['box'])) {
                    $querys->where('index.box', $inputs['box']);
                }
            });
        }
    }

    public function scopeSearch($query, $inputs) {
        if(!empty($inputs)){
            return $query->where(function ($querys) use ($inputs){
                if(!empty($inputs['mobile'])){
                    $querys->where('mobile', $inputs['mobile']);
                }
                if (!empty($inputs['company'])) {
                    $company_ids = Company::where('name', 'like', "%{$inputs['company']}%")
                        ->get()
                        ->pluck('id')
                        ->all();
                    $querys->whereIn('company_id', $company_ids);
                }
                if (!empty($inputs['index']) && is_array($inputs['index'])) {
                    sort($inputs['index']);
                    $querys->where([
                        'indexCache' => [
                            '$gte' => $inputs['index'][0],
                            '$lte' => $inputs['index'][1]
                        ]
                    ]);
                }
            });
        }
    }
}
